package org.example.jcclubsubjectdomain.basic.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.jcclubsubjectcommon.enums.IsDeletedFlagEnum;
import org.example.jcclubsubjectdomain.basic.convet.SubjectCategoryConverter;
import org.example.jcclubsubjectdomain.basic.entity.SubjectCategoryBo;
import org.example.jcclubsubjectdomain.basic.service.SubjectCategoryDomaoService;
import org.example.jcclubsubjectinfra.basic.entity.SubjectCategory;
import org.example.jcclubsubjectinfra.basic.service.SubjectCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 啊昌
 * @date 2025/4/12 10:26
 * SubjectCategoryDomainServiceImpl
 */
@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomaoService {
	@Autowired
	private SubjectCategoryService categoryService;


	@Override
	public List<SubjectCategoryBo> queryPrimaryCategory(SubjectCategoryBo subjectCategoryBo) {
		if(log.isInfoEnabled()){
			log.info("SubjectCategoryDomainServiceImpl.queryPrimaryCategory.bo:{}", subjectCategoryBo);
		}
//		把SubjectCategory bo 转化成SubjectCategory
		SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBo);
//		调用categoryService 模块查询 分类的list
		List<SubjectCategory> subjectCategoryList = categoryService.queryPrimaryCategory(subjectCategory);
//		模块查询 分类的list 是一个把SubjectCategory 通过 BeanUtils.copyProperties 复制到bo返回
		List<SubjectCategoryBo> collect = subjectCategoryList.stream().map(category -> {
			SubjectCategoryBo bo = new SubjectCategoryBo();
			BeanUtils.copyProperties(category, bo);
			return bo;
		}).collect(Collectors.toList());
		return collect;
	}

	@Override
	public boolean insert(SubjectCategoryBo subjectCategoryBo) {
		if (log.isInfoEnabled()) {
			log.info("SubjectCategoryDomainServiceImpl.add.bo:{}", subjectCategoryBo);
		}
		SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBo);
		subjectCategory.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
		subjectCategory.setCreatedTime(new Date());
		return categoryService.insert(subjectCategory);
	}
}
